﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using MicroFractal.iTunesBrowser.Core.Device;
using MicroFractal.iTunesBrowser.Business;

namespace MicroFractal.iTunesBrowser.Core.Data.SimpleFolderDB
{
    class SimpleFolderDBParser : DbParser
    {
        #region Constructors

        public SimpleFolderDBParser(IDevice device, MemoryStream file)
            : base(device, file)
        {
        }

        #endregion

        public override Library GetLibrary()
        {
            Library lib = new Library();
            foreach (string sd in Directory.GetDirectories(base._device.DBRootPath))
            {
                DirectoryInfo d = new DirectoryInfo(sd);
                Artist a = new Artist(d.Name);
                lib.Artists.Add(a);
                foreach (DirectoryInfo dd in d.GetDirectories())
                {
                    Album al = new Album(dd.Name, a);

                    lib.Albums.Add(al);
                    foreach (FileInfo file in dd.GetFiles())
                    {
                        if (LibraryManager.ImageExtensions.Contains(file.Extension.ToLower()))
                        {
                            al.Cover = new System.Windows.Media.Imaging.BitmapImage(new Uri(file.FullName));
                        }
                        if (LibraryManager.MusicExtensions.Contains(file.Extension.ToLower()))
                        {
                            Song s = new Song(file.Name, al);
                            s.Artist = a;
                            s.Location = file.FullName;
                            lib.Songs.Add(s);
                        }
                    }
                }
            }
            return lib;
        }

    }
}
